package web.language.helper

class DicUploadCommand {
    String dic
    String userId
}

class ImportentController {

    def upload = { DicUploadCommand duc ->

        def dicId = session.getAt("dicId")
        def dic = Dictionary.findById(dicId)

        String converted = duc.dic

        String[] entities = converted.split('\n')
        for (String ent in entities) {
            addEntity(ent, dic)
        }

        redirect controller: 'entry', action: 'list'//, id: duc.userId
    }

    def form = {
    }

    void addEntity(String ent, Dictionary dic) {
        String[] parts = ent.split('\t')
        def entry = new Entry(phrase: parts[0], translate: parts[1]);
        entry.setId('2')
        entry.save()
        dic.addToEntries(entry)
        dic.save(flash: true)
    }

}